#include "squid_zline.hpp"

#include "qbe/src/ecs/app.hpp"

#include "qbe/src/ecs/query.hpp"

#include "qbe/src/axis/axis.hpp"
#include "qbe/src/input/input.hpp"
#include "qbe/src/select/select.hpp"
#include "qbe/src/state/state.hpp"
#include "qbe/src/undoRedo/undoRedo.hpp"
#include "qbe/src/utils/utils.hpp"

#include "qbe/src/layout/layout.hpp"

#include "qlib/all.h"

#include "components.hpp"
#include "events.hpp"
#include "qbe/src/select/models.hpp"
#include "qbe/src/select/select_cmd.hpp"
#include "src/ql/common/myServiceEvent.h"

namespace qbe::squidZline {

void clear_tag(ecs::Query query) {
  auto& move_param = query.get_resource<Param>();

  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;

  auto view = reg.view<SquidZlineTag>();

  view.each([&reg](entt::entity e) { reg.remove<SquidZlineTag>(e); });
}

void clear_events(ecs::Query query) {
  // query.get_resource<ecs::Event<Event>>().clear();
}

// 退出移动模式时回调
void exit_callback(ecs::Query query, state::AllModeType mode) {
  // 获取移动参数
  auto& move_param = query.get_resource<Param>();

  // 判断移动模式是否在执行
  auto& layout_cmd = query.get_resource<layout::LayoutCmd>();
  auto event_writer = query.get_event_writer<Event>();

  // 获取reg
  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;

  clear_tag(query);

  auto& state = query.get_resource<state::State>();
  // state.switchMode(state::SelectModeType::Overall);

  move_param.reset();
  auto& events_writer = query.get_event_reader<Event>();
  event_writer.send({Code::End});

  state.switchMode(state::EditingChildMode::None);
}

void init(ecs::Commands commands, ecs::Query query) {
  Param param;
  commands.insert_resource(param);

  auto& state = query.get_resource<state::State>();
  state.addExitCall(state::EditingModeType::TOOL_SQUID, exit_callback);
}

// 退出模式
void exit(ecs::Query query) {
  // 获取移动参数
  auto& move_param = query.get_resource<Param>();

  auto& layout_cmd = query.get_resource<layout::LayoutCmd>();
  auto event_writer = query.get_event_writer<Event>();

  // 获取reg
  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;

  clear_tag(query);

  auto& select_cmd = query.get_resource<select::SelectCmd>();
  //清除预选择
  select_cmd.clear_hover();
  select_cmd.clear_hover_parts();
  // 退出模式
  auto& state = query.get_resource<state::State>();
  // state.switchMode(state::SelectModeType::Overall);
  state.exit();

  auto& partSelectMode = query.get_resource<select::SelectPartModel>();
  partSelectMode.hovers.clear();
  partSelectMode.selects.clear();

  auto& sele_event_writer = query.get_event_writer<select::Event>();
  sele_event_writer.send({select::Code::PartHoverCancel});
  sele_event_writer.send({select::Code::PartSelectCancel});

  move_param.reset();
  auto& events_writer = query.get_event_reader<Event>();
  event_writer.send({Code::End});
  state.switchMode(state::EditingChildMode::None);
}

// SQUID
void update(ecs::Query query, ecs::Bus& bus) {
  // 判断编辑模式为移动
  auto& state = query.get_resource<state::State>();
  auto& handModule = query.get_resource<vsg::ref_ptr<input::HandleBundle>>();
  auto& selectModels = query.get_resource<select::SelectPartModel>();
  auto& select_cmd = query.get_resource<select::SelectCmd>();
  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& axis = query.get_resource<axis::Axis>();
  auto& reg = cell->reg;

  if (state.getEditingMode() != state::EditingModeType::TOOL_SQUID) {
    return;
  }

  auto x = axis.real_x;
  auto y = axis.real_y;

  std::vector<qlib::Vec2d> res;
  if (handModule->mouseReleased(1)) {
    std::unordered_map<entt::entity, select::SelectPartData> res_border;
    select_cmd.query_nodes(x, y);
    select_cmd.get_part(x, y, res_border);
    if (!res_border.empty()) {
      select_cmd.add_hover_parts(res_border);
      for (auto it : res_border) {
        select::SelectPartData select_part = it.second;
        if (!select_part.edge_id_map.empty()) {
          auto edge_ids = select_part.edge_id_map.begin();
          res = select_part.get_edge(edge_ids->first);
          auto f = MyServiceEvent::getInstance()->useRegisterFunc<void(std::vector<qlib::Vec2d>&)>("Tool/SqiudZlineSelectLineOk");
          if (f) {
            f(res);
            exit(query);
          }
          break;
        }
      }
    }
  }
}

void Plugin::build(ecs::App& app) {
  app.add_stage<InitStage>();
  app.add_stage<UpdateStage>();

  app.init_event<Event>();

  app.add_systems<InitStage>(init);
  app.add_systems<UpdateStage>(update);
  app.add_systems<ecs::stage::Last>(clear_events);
}

}  // namespace qbe::squidZline